package portal.engine;

import java.io.IOException;
import java.util.concurrent.LinkedBlockingQueue;

import org.springframework.beans.factory.annotation.Autowired;

import beans.*;
import engine.BIEngine0_1.AlgorithmOperator;
import portal.dao.ScheduleJobDetailDao;
import portal.dao.ScheduleSocketLogDao;
import portal.util.SpringContextUtil;

public class UpdateJobThread extends Thread {
    public String token;
    //@Autowired
    private ScheduleJobDetailDao scheduleJobDetailDao;
    //@Autowired
    private ScheduleSocketLogDao scheduleSocketLogDao;
    

    public UpdateJobThread(){
    }
    public UpdateJobThread(ScheduleSocketLogDao sld,ScheduleJobDetailDao sjd){
    	this.scheduleJobDetailDao=sjd;
    	this.scheduleSocketLogDao=sld;
    }

    @Override
    public void run() {
    	if(scheduleSocketLogDao==null)
        this.scheduleSocketLogDao=SpringContextUtil.getBean(ScheduleSocketLogDao.class);
    	if(null==scheduleJobDetailDao)
        this.scheduleJobDetailDao=SpringContextUtil.getBean(ScheduleJobDetailDao.class);
        /**
         * 从服务器获取token
         */
        HttpResult tokenResult = JobOperator.getToken(HttpConfig.userName, HttpConfig.passWord);
        if (tokenResult.isResult()) {
            this.token = tokenResult.getMessage();
        }

        while (true) {
            LinkedBlockingQueue queues = JobQueue.getTaskQueue();
            Object[] jobIds = queues.toArray();

            for (Object object : jobIds) {
                String jobId = String.valueOf(object);
                HttpResultList result = null;
                try {
                    //获取作业详情
                    result = AlgorithmOperator.getJobFullState(jobId, token);
                } catch (IOException e) {
                    e.printStackTrace();
                }
                FullJobState fullJobState = (FullJobState) result.getDatas().get(0);
                String jobStatues = fullJobState.getStatus();
                String executionHost = fullJobState.getExecutionHost().split("]")[0].replaceAll("[\"\\[]", "");
                String terminationTime = fullJobState.getTerminationTime();
                String cpuUsage = "";
                String remainMem = "";
                //获取节点资源
                HttpResultList hostList = AlgorithmOperator.getJobBasicState(this.token);
                for (int j = 0; j < hostList.getDatas().size(); j++) {
                    BaseResourceState baseResourceState = (BaseResourceState) hostList.getDatas().get(j);
                    if (baseResourceState.getHostName().equals(executionHost)) {
                        cpuUsage = baseResourceState.getUt();
                        remainMem = baseResourceState.getMem();
                    }
                }
                Boolean updateJobDetail = scheduleJobDetailDao.updateScheduleJobDetail(jobId, jobStatues, terminationTime,
                        fullJobState.getMemUsage(), fullJobState.getCpuTime(), executionHost, cpuUsage, remainMem);
                Boolean updateSocketJob = scheduleSocketLogDao.updateJobStateByJobId(jobStatues, jobId);
                if (!(updateJobDetail && updateSocketJob)) {
                    System.out.println("作业详情更新失败");
                }else {
                    System.out.println("作业详情更新成功");
                }
                //如果作业不在运行中，将从队列中移除
                if (!(jobStatues.equals("RUN") || jobStatues.equals("RUNNING"))) {
                    queues.remove(jobId);
                }
            }
            try {
                Thread.sleep(30000);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
    }
}